﻿using System;
using System.Data;
using System.Windows.Forms;
using Infragistics.Win.UltraWinGrid.ExcelExport;

namespace Excel
{
    public partial class Form1 : Form
    {

        private int totalRowNum = 0;

        public Form1()
        {
            InitializeComponent();
            LoadGrid();
        }

        private DataTable GetTable()
        {
            var table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("STT", typeof(int));
            table.Columns.Add("MaSinhVien", typeof(string));
            table.Columns.Add("HoSinhVien", typeof(string));
            table.Columns.Add("TenSinhVien", typeof(string));
            table.Columns.Add("MaKhoa", typeof(string));
            table.Columns.Add("MaLop", typeof(string));
            return table;
        }

        private void LoadGrid()
        {
            try
            {
                var table = GetTable();
                var stt = 1;
                table.Rows.Add(1,1,785255,@"Nguyễn Quang",@"Khánh",@"CNTT",@"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");
                table.Rows.Add(1, 1, 785255, @"Nguyễn Quang", @"Khánh", @"CNTT", @"55PM");

                UltraGrid1.DataSource = table;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                

            }
        }

        private void ultraGridExcelExporter1_EndExport(object sender, EndExportEventArgs e)
        {
            //this.totalRowNum = e.CurrentRowIndex;
        }

        private void ExportToFile_Click(object sender, EventArgs e)
        {
            try
            {
                ultraGridExcelExporter1.Export(UltraGrid1, "GridData.xls");

                TimeSpan timeSpan = DateTime.Now - start;

                MessageBox.Show("Exported " + totalRowNum + " rows in " + timeSpan.Seconds + "sec : "
                    + timeSpan.Milliseconds + "msec \n\nFile: " + exportFileName.Text);
            }
            catch
            {
                MessageBox.Show("Specified Path Does Not Exist", "Invalid File Name");
            }
        }

        private void btnExportxlsxFile_Click(object sender, EventArgs e)
        {
            DateTime start = DateTime.Now;

            try
            {
                ultraGridExcelExporter1.Export(this.UltraGrid1, @"C:\GridData.xlsx", Infragistics.Documents.Excel.WorkbookFormat.Excel2007);

                TimeSpan timeSpan = DateTime.Now - start;

                MessageBox.Show("Exported " + totalRowNum + " rows in " + timeSpan.Seconds + "sec : "
                    + timeSpan.Milliseconds + "msec \n\nFile: " + txtExcelFileNamexlsx.Text);
            }
            catch
            {
                MessageBox.Show("Specified Path Does Not Exist", "Invalid File Name");
            }
        }
    }
}
